package cn.itcast.mq.spring;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

@Component
public class SpringRabbitListener {

    //@RabbitListener(queues = "simple.queue")
    //public void listenSimpleQueue(String message) {
    //    System.out.println("收到消息："+message);
    //}


    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue1(String message) throws InterruptedException {
        System.out.println("consumer1 ----> 收到消息："+message + " " + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue2(String message) throws InterruptedException {
        System.err.println("consumer2 ----> 收到消息："+message + " " + LocalTime.now());
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) throws InterruptedException {
        System.out.println("consumer ----> 收到fanout.queue1消息：" + message);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) throws InterruptedException {
        System.out.println("consumer ----> 收到fanout.queue2消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("direct.queue1"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}))
    public void listenDirectQueue1(String message) throws InterruptedException {
        System.out.println("consumer1 ----> 收到direct.queue1消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("direct.queue2"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}))
    public void listenDirectQueue2(String message) throws InterruptedException {
        System.out.println("consumer2 ----> 收到direct.queue2消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic.queue1"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"))
    public void listenTopicQueue1(String message) throws InterruptedException {
        System.out.println("consumer1 ----> 收到topic.queue1消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic.queue2"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"))
    public void listenTopicQueue2(String message) throws InterruptedException {
        System.out.println("consumer2 ----> 收到topic.queue2消息：" + message);
    }


    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue1(Map<String, String> map) throws InterruptedException {
        System.out.println("consumer1 ----> 收到消息："+map + " " + LocalTime.now());
        Thread.sleep(20);
    }
}
